package com.deguru.main.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.deguru.main.dto.User;
import com.deguru.main.service.UserService;
import com.deguru.main.service.impl.UserServiceImpl;
import com.deguru.main.utils.GetVerifyCode;

@Controller()
@RequestMapping("/customer")
public class CustomerController {

	UserService userService = new UserServiceImpl();

	@RequestMapping("/home")
	public String toHomePage() {
		return "index";
	}

	@RequestMapping("/login")
	public String toLoginPage() {
		return "login";
	}

	@RequestMapping("/login.do")
	public String userLogin(@RequestParam("userId") String userId, @RequestParam("pswd") String pswd,
			HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		User user = userService.selectByUserIdAndPassword(userId, pswd);
		if (user != null) {
			req.setAttribute("tip", "登录成功！");
			req.getSession().setAttribute("session_user", user);
			return "index";
		}
		return "login";
	}
	
	@RequestMapping("/reg")
	public String toRegPage() {
		return "reg";
	}

	@RequestMapping("/reg.do")
	public String userRegister(@RequestParam("name") String userId, @RequestParam("pwd") String pswd,
			@RequestParam("comfirm_pwd") String confirm, @RequestParam("email") String email,
			@RequestParam("phone") String phone, @RequestParam("code") String code, HttpServletRequest req) {
		HttpSession session = req.getSession();
		String text = (String) session.getAttribute("verifyCodeText");
		User user = new User(userId,pswd,email,phone);
		System.out.println(user);
		Integer count = userService.insertNewUser(user);
		if (count > 0) {
			System.out.println("Reg.do====Success!");
			req.setAttribute("tip", "注册成功，请登录！");
			return "login";
		} else {
			req.setAttribute("tip", "注册失败，请重试！");
			return "reg";
		}
	}

	@RequestMapping("/checkUID.do")
	public void checkUID(@RequestParam("name") String userId, HttpServletResponse resp) throws IOException {
		if (userId.length() >= 6 && userId.length() <= 16) {
			boolean uidFlag = userService.selectByUserId(userId);
			if (uidFlag) {
				resp.getWriter().append("true");
			} else {
				resp.getWriter().append("false");
			}
		} else {
			resp.getWriter().append("null");
		}
	}

	@RequestMapping("/checkVC.do")
	public void checkVC(@RequestParam("verifyCode") String vc, HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		vc = (vc == null ? "" : vc);
		String vcInput = vc.toUpperCase();
		String vcText = req.getSession().getAttribute("verifyCodeText").toString().toUpperCase();
		if (!vcText.equals(vcInput)) {
			resp.getWriter().append("false");
		} else {
			resp.getWriter().append("true");
		}
	}

	@RequestMapping("/getVC.do")
	public void getVC(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		GetVerifyCode vc = new GetVerifyCode(); // 用我们的验证码类，生成验证码类对象
		BufferedImage image = vc.getImage(); // 获取验证码
		req.getSession().setAttribute("verifyCodeText", vc.getText()); // 将验证码的文本存在session中
		GetVerifyCode.output(image, resp.getOutputStream());// 将验证码图片响应给客户端
	}
}
